package com.basketball.gym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.basketball.gym.mapper.UserMapper;
import com.basketball.gym.pojo.Calendar;
import com.basketball.gym.mapper.CalendarMapper;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.state.ResponseState;
import com.basketball.gym.service.CalendarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basketball.gym.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.interfaces.RSAKey;
import java.sql.Timestamp;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 赵肖云
 * @since 2021-03-26
 */
@Service
public class CalendarServiceImpl extends ServiceImpl<CalendarMapper, Calendar> implements CalendarService {
    @Autowired
    private CalendarMapper calendarMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询球馆日程
     */
    @Override
    @Transactional
    public ResponseData<List<Calendar>> getGymCalendar(Calendar calendar) {
        List<Calendar> calendars = null;
        if (calendar.getType() == 2) {
            calendars = calendarMapper.selectList(getCalendarQueryWrapper(calendar));
            //当天数据表未创建数据时创建数据
            if (calendar.getDate() != null) {
                if (calendars == null || calendars.size() == 0) {
                    for (int i = 1; i < 5; i++) {
                        for (int j = 1; j < 13; j++) {
                            calendarMapper.insert(new Calendar(j, calendar.getDate(), i));
                        }
                    }
                    calendars = calendarMapper.selectList(getCalendarQueryWrapper(calendar));
                }
            }
            //赋值type
            for (Calendar nowCalendar : calendars) {
                nowCalendar.setType(calendar.getType());
            }
        } else if (calendar.getType() == 3) {
            //判断球场ID
            List<Integer> courtIdList = new ArrayList<>();
            //5表示1、2半场
            if (calendar.getCourtId() != null && calendar.getCourtId() == 5) {
                courtIdList.add(1);
                //6表示3、4半场
            } else if (calendar.getCourtId() != null && calendar.getCourtId() == 6) {
                courtIdList.add(3);
                //null时都查询
            } else {
                courtIdList.add(1);
                courtIdList.add(3);
            }
            calendar.setCourtIdList(courtIdList);
            calendars = calendarMapper.selectAllPresent(calendar);
            //当天数据表未创建数据时创建数据
            if (calendar.getDate() != null) {
                if (calendars == null || calendars.size() == 0) {
                    for (int i = 1; i < 5; i++) {
                        for (int j = 1; j < 13; j++) {
                            calendarMapper.insert(new Calendar(j, calendar.getDate(), i));
                        }
                    }
                    calendars = calendarMapper.selectAllPresent(calendar);
                }
            }
            //整合两个半场的状态得出整场的状态
            for (Calendar nowCalendar : calendars) {
                if (nowCalendar.getBState() != 1) {
                    nowCalendar.setState(nowCalendar.getBState());
                }
                nowCalendar.setCourtId(calendar.getCourtId());
            }
        }
        if (calendars != null) {
            for (Calendar thisCalendar : calendars) {
                thisCalendar.setDisable(thisCalendar.getState() != 1);
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), calendars);
    }

    /**
     * 修改日程状态
     */
    @Override
    @Transactional
    public ResponseData<Object> updateCalendarState(Calendar calendar) {
        Calendar firstCalendar = calendarMapper.selectById(calendar.getId());
        if (firstCalendar == null) {
            return new ResponseData<>(ResponseState.CALENDAR_NOT_EXIST.getMessage(), ResponseState.CALENDAR_NOT_EXIST.getValue());
        }
        firstCalendar.setState(calendar.getState());
        calendarMapper.updateById(firstCalendar);
        //全场时修改另一个半场的状态
        if (calendar.getType() == 3) {
            Calendar nextCalendar = getOtherCalendar(firstCalendar);
            nextCalendar.setState(calendar.getState());
            calendarMapper.updateById(nextCalendar);
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 管理员查询日程
     */
    @Override
    public ResponseData<List<Calendar>> adminGetGymCalendar(Calendar calendar) {
        List<Calendar> calendars = null;
        if (calendar.getType() == 2) {
            calendars = calendarMapper.selectList(getCalendarQueryWrapper(calendar));
            //当天数据表未创建数据时创建数据
            if (calendar.getDate() != null) {
                if (calendars == null || calendars.size() == 0) {
                    for (int i = 1; i < 5; i++) {
                        for (int j = 1; j < 13; j++) {
                            calendarMapper.insert(new Calendar(j, calendar.getDate(), i));
                        }
                    }
                    calendars = calendarMapper.selectList(getCalendarQueryWrapper(calendar));
                }
            }
            //赋值type
            for (Calendar nowCalendar : calendars) {
                nowCalendar.setType(calendar.getType());
            }
        } else if (calendar.getType() == 3) {
            //判断球场ID
            List<Integer> courtIdList = new ArrayList<>();
            //5表示1、2半场
            if (calendar.getCourtId() != null && calendar.getCourtId() == 5) {
                courtIdList.add(1);
                //6表示3、4半场
            } else if (calendar.getCourtId() != null && calendar.getCourtId() == 6) {
                courtIdList.add(3);
                //null时都查询
            } else {
                courtIdList.add(1);
                courtIdList.add(3);
            }
            calendar.setCourtIdList(courtIdList);
            calendars = calendarMapper.selectAllPresent(calendar);
            //当天数据表未创建数据时创建数据
            if (calendar.getDate() != null) {
                if (calendars == null || calendars.size() == 0) {
                    for (int i = 1; i < 5; i++) {
                        for (int j = 1; j < 13; j++) {
                            calendarMapper.insert(new Calendar(j, calendar.getDate(), i));
                        }
                    }
                    calendars = calendarMapper.selectAllPresent(calendar);
                }
            }
            //整合两个半场的状态得出整场的状态
            for (Calendar nowCalendar : calendars) {
                if (nowCalendar.getBState() != 1) {
                    nowCalendar.setState(nowCalendar.getBState());
                }
                if(calendar.getCourtId()!=null){
                    nowCalendar.setCourtId(calendar.getCourtId());
                }else {
                    if(nowCalendar.getCourtId()==1){
                        nowCalendar.setCourtId(5);
                    }else if(nowCalendar.getCourtId()==3){
                        nowCalendar.setCourtId(6);
                    }
                }

            }
        }
        if (calendars != null) {
            for (Calendar thisCalendar : calendars) {
                thisCalendar.setDisable(thisCalendar.getState() != 1);
                if(thisCalendar.getUserId()!=null){
                    thisCalendar.setUser(userMapper.selectById(thisCalendar.getUserId()));
                }
            }
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), calendars);

    }

    /**
     * 获取查询日程的QueryWrapper
     */
    private QueryWrapper<Calendar> getCalendarQueryWrapper(Calendar calendar) {
        QueryWrapper<Calendar> queryWrapper = new QueryWrapper<>();
        if (calendar.getTimeId() != null) {
            queryWrapper.eq("time_id", calendar.getTimeId());
        }
        if (calendar.getCourtId() != null) {
            queryWrapper.eq("court_id", calendar.getCourtId());
        }
        if (calendar.getDate() != null) {
            queryWrapper.eq("date", calendar.getDate());
        }
        if (calendar.getState() != null) {
            queryWrapper.eq("state", calendar.getState());
        }
        return queryWrapper;
    }

    /**
     * 获得另一半场的日程
     */
    private Calendar getOtherCalendar(Calendar calendar) {
        calendar.setCourtId(calendar.getCourtId() + 1);
        QueryWrapper<Calendar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("court_id", calendar.getCourtId());
        queryWrapper.eq("time_id", calendar.getTimeId());
        queryWrapper.eq("date", calendar.getDate());
        return calendarMapper.selectOne(queryWrapper);
    }
}
